package com.supermap.util.base;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


import android.annotation.SuppressLint;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.supermap.pisaclient.R;
import com.supermap.util.util.ResourcesUtil;
import com.supermap.util.widget.LazyViewPager;


/**
 * Copyright (C) 2017 北京超图软件股份有限公司 版权所有
 * <p>
 * 功能描述：所有TabActivity的基类 作者：李会林
 * <p>
 * 修改人： 修改描述： 修改日期
 */

public abstract class BaseTabActivity extends BaseActivity implements OnCheckedChangeListener {
	public final int TYPE_NORMAL = 0; // 普通顶部滑动tab,右边无红点提示
	public final int TYPE_ROUND_POINT = 1; // 普通顶部滑动tab,右边有红点提示
	public final int TYPE_TOP_NO_SCROLL = 2; // 顶部不允许滑动tab，只允许点击
	public final int TYPE_TOP_ROUND_NO_SCROLL = 3; // 顶部不允许滑动tab,并且有红点提示，只允许点击
	public final int TYPE_BUTTOM_NO_SCROLL = 4; // 底部tab，不允许滑动，和主界面一样
	public final int TYPE_TAB_HAS_DRAWABLE = 5; // tab旁边有图标，具体位置根据传入的值决定
	public final int TYPE_MIDDLE_ACTION = 6; // tab中间有一个操作按钮

	private LazyViewPager mViewPager;

	private ImageView mIvLine;
	private LinearLayout mTxtContent;
	private String[] mTxtTitles;
	private List<Boolean> mNeedRoundPoints;
	private int mStartDelta;// 用于保存红色横线动画的起始位置
	private int mInitPosition;// 初始化的时候红色横线的位置
	private int[] mTxtColors;
	private int[] mTxtDrawable;
	private float mEachText; // 每个字相差多大
	private int mEachItemWidth; // 每个tab宽度

	private List<Boolean> mNeedNewFlags;

	private LinearLayout mTopTabContent;
	private LinearLayout mButtomContent;
	private LinearLayout mTopFixedContent;
	private LinearLayout mHeaderContent;

	private LinearLayout mButtomToolBar;
	private RadioGroup mButtonTabs;
	private ImageView mMiddleAction;

	private RelativeLayout mBtnToolBar;
	private LinearLayout mBtnContainer;
	private View mMidBtnHolder;
	private int mType = TYPE_NORMAL;// 0为普通样式

	private int mCurrentPosition;
	private int mLastPosition;
	private Map<Integer, Fragment> mFragmentList;
	private TabFragmentPagerAdapter mAdapter;

	public void setInitPosition(int initPosition) {
		mInitPosition = initPosition;
	}

	/**
	 * 
	 * @param position
	 */
	public void onPageChanged(int position) {
	}

	@Override
	public void addViewIntoContent() {
		addView(R.layout.activity_base_tab);
		setTabActivityBackground();
		initDefaultView();
		init();
	}

	public LinearLayout getTopFixedContainer() {
		return mTopFixedContent;
	}

	public LinearLayout getHeaderContainer() {
		return mHeaderContent;
	}

	public LinearLayout getBottomToolBar() {
		return mButtomToolBar;
	}

	private void initDefaultView() {
		mEachText = ResourcesUtil.getDimension(R.dimen.px3);
		mViewPager = (LazyViewPager) findViewById(R.id.tab_content_view_pager);
		mIvLine = (ImageView) findViewById(R.id.tab_scroll_img);
		mTxtContent = (LinearLayout) findViewById(R.id.txt_content);
		mTopTabContent = (LinearLayout) findViewById(R.id.top_tab_content);
		mButtomContent = (LinearLayout) findViewById(R.id.buttom_content);
		mTopFixedContent = (LinearLayout) findViewById(R.id.top_fixed_content);
		mHeaderContent = (LinearLayout) findViewById(R.id.header_content);
		mButtonTabs = (RadioGroup) findViewById(R.id.buttom_tab_content);
		mButtomToolBar = (LinearLayout) findViewById(R.id.bottom_tool_bar);
		mBtnToolBar = (RelativeLayout) findViewById(R.id.buttom_btn_content);
		mBtnContainer = (LinearLayout) findViewById(R.id.btn_container);
		mMiddleAction = (ImageView) findViewById(R.id.middle_action);
		mButtonTabs.setOnCheckedChangeListener(this);
		mFragmentList = new HashMap<Integer, Fragment>();
		mViewPager.setOnPageChangeListener((LazyViewPager.OnPageChangeListener) new MyOnPageChangeListener());
		mTxtContent.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
			@Override
			public boolean onPreDraw() {
				mTxtContent.getViewTreeObserver().removeOnPreDrawListener(this);
				if (mType != TYPE_BUTTOM_NO_SCROLL && mType != TYPE_MIDDLE_ACTION) {
					changeText(getPos());
				}
				return true;
			}
		});
	}

	public abstract void init();

	/**
	 * 设置样式,在init()里使用
	 */
	protected void setType(int type) {
		mType = type;
		// if (type == TYPE_BUTTOM_NO_SCROLL) {
		// android.widget.FrameLayout.LayoutParams params = new
		// android.widget.FrameLayout.LayoutParams(
		// android.view.ViewGroup.LayoutParams.MATCH_PARENT,
		// android.view.ViewGroup.LayoutParams.MATCH_PARENT);
		// params.topMargin = 0;
		// mViewPager.setLayoutParams(params);
		// }
		if (type == TYPE_BUTTOM_NO_SCROLL || type == TYPE_TOP_ROUND_NO_SCROLL || type == TYPE_MIDDLE_ACTION) {
			mViewPager.setSrollState(false);
		}
	}

	protected void enableTabScroll() {
		mViewPager.setSrollState(true);
	}

	/**
	 * 设置是否需要圆点
	 */
	protected void setNeedRoundPoint(List<Boolean> isNeeds) {
		if (mType == TYPE_ROUND_POINT || mType == TYPE_TOP_ROUND_NO_SCROLL) {
			mNeedRoundPoints = isNeeds;
			for (int i = 0; i < mTxtContent.getChildCount(); i++) {
				if (mNeedRoundPoints != null) {
					if (mNeedRoundPoints.get(i)) {
						((TextView) mTxtContent.getChildAt(i).findViewById(R.id.title_item_text))
								.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.unread_point, 0);
					} else {
						((TextView) mTxtContent.getChildAt(i).findViewById(R.id.title_item_text))
								.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
					}
				}
			}
		}
	}

	protected void setNeedNewFlags(List<Boolean> isNeeds) {
		if (mType == TYPE_MIDDLE_ACTION) {
			mNeedNewFlags = isNeeds;
			if (mNeedNewFlags != null) {
				int j = 0;
				final int size = mBtnContainer.getChildCount();
				for (int i = 0; i < size; i++) {
					if (i != size / 2) {
						j = i > size / 2 ? i - 1 : i;
						ImageView iv = (ImageView) mBtnContainer.getChildAt(i).findViewById(R.id.new_flag);
						if (iv != null) {
							iv.setVisibility(mNeedNewFlags.get(j) ? View.VISIBLE : View.GONE);
						}
					}
				}
			}
		}
	}

	/**
	 * 设置tab右边的drawable,此方法调用必须在addView方法调用之后再调用 否则无法显示图标 调用顺序应该是在init中依次调用
	 * addView()-->setType(int)-->setTabDrawable
	 */
	protected void setTabDrawable(List<Integer> drawableIds, int[] positions) {
		if (drawableIds == null || positions == null || drawableIds.size() != positions.length) {
			throw new IllegalArgumentException("please check the args");
		}
		if (mType != TYPE_TAB_HAS_DRAWABLE) {
			throw new IllegalArgumentException("plese set type to TYPE_TAB_HAS_DRAWABLE int init()");
		}
		if (mTxtContent.getChildCount() == 0) {
			return;
		}
		for (int i = 0; i < positions.length; i++) {
			TextView text = (TextView) mTxtContent.getChildAt(positions[i]).findViewById(R.id.title_item_text);
			if (text == null) {
				try {
					throw new Exception("tab TextView is null");
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			} else {
				text.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableIds.get(i), 0);
			}
		}
		setImageViewDefault();
	}

	/**
	 * 初始化无初始参数界面调用 用于可滑动tab
	 * 
	 * @param tabTxt
	 * @param list
	 * @throws Exception
	 */
	public void addViews(String[] tabTxt, List<Class<? extends Fragment>> list) throws Exception {
		if (tabTxt == null || tabTxt.length <= 0) {
			throw new Exception(" tabs text is null");
		} else if (list == null || list.size() == 0) {
			throw new Exception(" fragment is null");
		} else if (tabTxt.length != list.size()) {
			throw new Exception("tab text length is not equals fragment list size");
		} else {
			mTxtTitles = tabTxt;
			createTabItem();
			setImageViewDefault();
			mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list);
			mViewPager.setAdapter(mAdapter);
		}
	}

	/**
	 * 初始化需传入参数的界面调用 用于可滑动tab
	 * 
	 * @param <T>
	 * @param tabTxt
	 * @param list
	 * @param argList
	 *            Integer:需要设置参数的Fragment位置 Map<String, T> String 方法名 T 参数值
	 * @throws Exception
	 */
	public <T> void addViews(String[] tabTxt, List<Class<? extends Fragment>> list,
			Map<Integer, Map<String, T>> argList) throws Exception {
		if (tabTxt == null || tabTxt.length <= 0) {
			throw new Exception(" tabs text is null");
		} else if (list == null || list.size() == 0) {
			throw new Exception(" fragment is null");
		} else if (tabTxt.length != list.size()) {
			throw new Exception("tab text length is not equals fragment list size");
		} else if (argList == null || argList.size() == 0) {
			throw new IllegalArgumentException("method list is null or size is 0; arg list is null or size is 0");
		} else {
			mTxtTitles = tabTxt;
			createTabItem();
			setImageViewDefault();
			mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list, argList);
			mViewPager.setAdapter(mAdapter);
		}
	}

	/**
	 * 初始化无初始参数界面调用 用于不可滑动tab
	 * 
	 * @param tabTxt
	 * @param drawable
	 *            用于底部tab文字上面的图标的selector, 若没有图标，则可以传入null
	 * @param txtColors
	 *            tab文字对应的颜色，可以是selector，如果传入null，则使用默认的颜色
	 * @param list
	 * @throws Exception
	 */
	public void addViews(String[] tabTxt, int[] txtColors, int[] drawable, List<Class<? extends Fragment>> list)
			throws Exception {
		if (tabTxt == null || tabTxt.length <= 0) {
			throw new NullPointerException(" tabs text is null");
		} else if (list == null || list.size() == 0) {
			throw new NullPointerException(" fragment is null");
		} else if (tabTxt.length != list.size()) {
			throw new Exception(
					"tab text length is not equals fragment list size or tab text length is not equals drawable");
		} else {
			mTxtTitles = tabTxt;
			mTxtColors = txtColors;
			mTxtDrawable = drawable;
			createTabItem();
			mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list);
			mViewPager.setAdapter(mAdapter);
		}
	}

	/**
	 * 初始化有初始参数界面调用 用于不可滑动tab
	 * 
	 * @param tabTxt
	 * @param drawable
	 *            用于底部tab文字上面的图标的selector, 若没有图标，则可以传入null
	 * @param txtColors
	 *            tab文字对应的颜色，可以是selector，如果传入null，则使用默认的颜色
	 * @param list
	 * @throws Exception
	 */
	public <T> void addViews(String[] tabTxt, int[] txtColors, int[] drawable, List<Class<? extends Fragment>> list,
			Map<Integer, Map<String, T>> argList) throws Exception {
		if (tabTxt == null || tabTxt.length <= 0) {
			throw new NullPointerException(" tabs text is null");
		} else if (list == null || list.size() == 0) {
			throw new NullPointerException(" fragment is null");
		} else if (tabTxt.length != list.size()) {
			throw new Exception(
					"tab text length is not equals fragment list size or tab text length is not equals drawable");
		} else if (argList == null || argList.size() == 0) {
			throw new IllegalArgumentException("method list is null or size is 0; arg list is null or size is 0");
		} else {
			mTxtTitles = tabTxt;
			mTxtColors = txtColors;
			mTxtDrawable = drawable;
			createTabItem();
			mAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), list, argList);
			mViewPager.setAdapter(mAdapter);
		}
	}

	public void updateText(int pos, String newTitle) {
		if (pos >= 0 && pos < mTxtTitles.length) {
			mTxtTitles[pos] = newTitle;
			createTabItem();
			changeText(pos);
			setImagePosition(pos);
		}
	}

	public void changeText(int position) {
		if (mTxtTitles != null) {
			for (int i = 0; i < mTxtTitles.length; i++) {
				TextView textView = (TextView) mTxtContent.getChildAt(i).findViewById(R.id.title_item_text);
				if (position == i) {
					textView.setTextColor(ResourcesUtil.getColor(R.color.common_title));
					textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResourcesUtil.getDimension(R.dimen.font_34px));
				} else {
					textView.setTextColor(ResourcesUtil.getColor(R.color.black_deep_font));
					textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, ResourcesUtil.getDimension(R.dimen.font_28px));
				}
			}
		}
	}

	public void hideMidBtn() {
		if (mMidBtnHolder != null) {
			mMidBtnHolder.setVisibility(View.GONE);
		}
		if (mMiddleAction != null) {
			mMiddleAction.setVisibility(View.GONE);
		}
	}

	// 仅仅初始化的时候调用
	private void setImageViewDefault() {
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		LayoutParams params = (LayoutParams) mIvLine.getLayoutParams();
		mEachItemWidth = dm.widthPixels / mTxtTitles.length;

		if (mTxtTitles.length <= 2) {
			params.width = mEachItemWidth - ResourcesUtil.getDimensionPixelOffset(R.dimen.px60);
		} else {
			params.width = mEachItemWidth;
		}

		mIvLine.setLayoutParams(params);

		int itemSpacing = 0;
		itemSpacing = (mEachItemWidth - params.width) / 2;

		int endDelta = itemSpacing + mInitPosition * mEachItemWidth;
		Animation animation = new TranslateAnimation(endDelta, endDelta, 0, 0);
		animation.setFillAfter(true);
		mIvLine.startAnimation(animation);
		mStartDelta = endDelta;
	}

	// 切换viewpager的时候调用
	private void setImagePosition(int position) {
		LayoutParams params = (LayoutParams) mIvLine.getLayoutParams();
		int endDelta = mEachItemWidth * position + (mEachItemWidth - params.width) / 2;
		Animation animation = new TranslateAnimation(mStartDelta, endDelta, 0, 0);
		animation.setFillAfter(true);
		animation.setDuration(300);
		mIvLine.startAnimation(animation);
		mCurrentPosition = position;
		mStartDelta = endDelta;
	}

	private void createTabItem() {
		switch (mType) {
		case TYPE_NORMAL:
			createNormalTab();
			break;
		case TYPE_ROUND_POINT:
			createRoundPointType();
			break;
		case TYPE_TOP_NO_SCROLL:
			createNormalTab();
			setImageViewDefault();
			break;
		case TYPE_TOP_ROUND_NO_SCROLL:
			createRoundPointType();
			setImageViewDefault();
			break;
		case TYPE_BUTTOM_NO_SCROLL:
			mTopTabContent.setVisibility(View.GONE);
			mButtomContent.setVisibility(View.VISIBLE);
			createButtomTabItems();
			break;
		case TYPE_MIDDLE_ACTION:
			mTopTabContent.setVisibility(View.GONE);
			mBtnToolBar.setVisibility(View.VISIBLE);
			mCurrentPosition = -1;
			mLastPosition = -1;
			createBtnTabItems();
			break;
		}
	}

	@SuppressLint("InflateParams")
	private void createButtomTabItems() {
		LayoutInflater inflater = getLayoutInflater();
		for (int i = 0; i < mTxtTitles.length; i++) {
			RadioButton child = (RadioButton) inflater.inflate(R.layout.layout_buttom_radiobutton, null);
			RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(0,
					ViewGroup.LayoutParams.MATCH_PARENT, 1);
			child.setLayoutParams(params);
			child.setId(i);
			child.setText(mTxtTitles[i]);
			if (mTxtDrawable != null && mTxtDrawable.length > i) {
				child.setCompoundDrawablesWithIntrinsicBounds(0, mTxtDrawable[i], 0, 0);
			}
			if (mTxtColors != null && mTxtColors.length > i) {
				child.setTextColor(ResourcesUtil.getColor(mTxtColors[i]));
			}
			if (i == 0) {
				child.performClick();
			}
			mButtonTabs.addView(child);
		}
	}

	private void refreshTexts() {
		if (mType == TYPE_MIDDLE_ACTION) {
			int j = 0;
			int num = mBtnContainer.getChildCount();
			for (int i = 0; i < num; i++) {
				final RadioButton btnView = (RadioButton) mBtnContainer.getChildAt(i).findViewById(R.id.btn_item);
				if (btnView != null) {
					btnView.setText(mTxtTitles[j]);
					if (j == mCurrentPosition && !btnView.isChecked()) {
						btnView.setChecked(true);
					}
					j++;
				}
			}
		}
	}

	private void createBtnTabItems() {
		LayoutInflater inflater = getLayoutInflater();
		int len = mTxtTitles.length;
		mBtnContainer.removeAllViews();
		for (int i = 0; i < len; i++) {
			if (i == len / 2) {
				// 占位
				View v = new View(this);
				LayoutParams param = new LayoutParams(
						ResourcesUtil.getDimensionPixelSize(R.dimen.px150), LayoutParams.MATCH_PARENT);
				v.setLayoutParams(param);
				mBtnContainer.addView(v);
				mMidBtnHolder = v;
			}
			RelativeLayout child = (RelativeLayout) inflater.inflate(R.layout.layout_buttom_button, null);
			final RadioButton btnView = (RadioButton) child.findViewById(R.id.btn_item);
			final ImageView newFlag = (ImageView) child.findViewById(R.id.new_flag);
			LayoutParams params = new LayoutParams(0, LayoutParams.MATCH_PARENT,
					1);
			child.setLayoutParams(params);
			child.setId(i);
			btnView.setText(mTxtTitles[i]);
			if (mTxtDrawable != null && mTxtDrawable.length > i) {
				btnView.setCompoundDrawablesWithIntrinsicBounds(0, mTxtDrawable[i], 0, 0);
			}
			if (mTxtColors != null && mTxtColors.length > i) {
				btnView.setTextColor(ResourcesUtil.getColor(mTxtColors[i]));
			}
			if (mNeedNewFlags != null) {
				newFlag.setVisibility(mNeedNewFlags.get(i) ? View.VISIBLE : View.GONE);
			}
			final int index = i;
			btnView.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					if (isChecked) {
						updateBottomItemChecked(buttonView, index);
					}
				}

			});
			if (i == 0) {
				btnView.performClick();
			}
			btnView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (mLastPosition == index) {
						handleItemClickAgain(index);
					}
					mLastPosition = index;
				}

			});
			mBtnContainer.addView(child);
		}
		mMiddleAction.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				handleMiddleActionClick();
			}

		});
	}

	protected void handleItemClickAgain(int index) {
		// ToastUtil.showCustomViewToast(this, "click btn " + index);
	}

	protected void handleMiddleActionClick() {
		// ToastUtil.showCustomViewToast(this, "click middle btn!");
	}

	public void setCurrentTabForMiddleAction(int position) {
		if (mType == TYPE_MIDDLE_ACTION && position >= 0 && position < mViewPager.getAdapter().getCount()) {
			final int size = mBtnContainer.getChildCount();
			// 记录radiobutton的位置
			int j = 0;
			for (int i = 0; i < size; ++i) {
				View v = mBtnContainer.getChildAt(i);
				RadioButton btnView = (RadioButton) v.findViewById(R.id.btn_item);
				if (btnView != null) {
					if (j == position) {
						btnView.setChecked(true);
						break;
					}
					j++;
				}

			}

		}
	}

	private void updateBottomItemChecked(CompoundButton buttonView, int index) {
		final int size = mBtnContainer.getChildCount();
		for (int i = 0; i < size; ++i) {
			View v = mBtnContainer.getChildAt(i);
			RadioButton btnView = (RadioButton) v.findViewById(R.id.btn_item);
			if (btnView != null && btnView != buttonView) {
				btnView.setChecked(false);
			}
		}
		mViewPager.setCurrentItem(index);
		mLastPosition = mCurrentPosition;
		mCurrentPosition = index;
	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		mViewPager.setCurrentItem(checkedId);
		mCurrentPosition = checkedId;
	}

	private void setCurrentBottomTab(int position) {
		if (mType == TYPE_BUTTOM_NO_SCROLL) {
			int count = mButtonTabs.getChildCount();
			for (int i = 0; i < count; i++) {
				RadioButton radio = (RadioButton) mButtonTabs.getChildAt(i);
				if (i == position) {
					radio.performClick();
					break;
				}
			}
		}
	}

	// 创建普通样式的tab
	private void createNormalTab() {
		mTxtContent.removeAllViews();
		LayoutInflater inflater = getLayoutInflater();
		for (int i = 0; i < mTxtTitles.length; i++) {
			View view = inflater.inflate(R.layout.layout_tab_title_item, null);
			TextView txtView = (TextView) view.findViewById(R.id.title_item_text);
			txtView.setText(mTxtTitles[i]);
			LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
			view.setLayoutParams(params);
			final int position = i;
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					mViewPager.setCurrentItem(position);
				}
			});
			mTxtContent.addView(view);
		}
	}

	// 创建有圆点样式的tab
	private void createRoundPointType() {
		mTxtContent.removeAllViews();
		LayoutInflater inflater = getLayoutInflater();
		for (int i = 0; i < mTxtTitles.length; i++) {
			View view = inflater.inflate(R.layout.layout_tab_title_item, null);
			TextView txtView = (TextView) view.findViewById(R.id.title_item_text);
			txtView.setText(mTxtTitles[i]);
			if (mNeedRoundPoints != null) {
				if (mNeedRoundPoints.get(i)) {
					txtView.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.img_ren_point, 0);
				}
			}
			LayoutParams params = new LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1);
			view.setLayoutParams(params);
			final int position = i;
			view.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					mViewPager.setCurrentItem(position);
				}
			});
			mTxtContent.addView(view);
		}
	}

	private class TabFragmentPagerAdapter<T> extends FragmentPagerAdapter {
		List<Class<? extends Fragment>> listClass;
		Map<Integer, Map<String, T>> argList;

		public TabFragmentPagerAdapter(FragmentManager fm, List<Class<? extends Fragment>> list) {
			super(fm);
			listClass = list;
		}

		public TabFragmentPagerAdapter(FragmentManager fm, List<Class<? extends Fragment>> list, Fragment fragment) {
			super(fragment.getChildFragmentManager());
			listClass = list;
		}

		public TabFragmentPagerAdapter(FragmentManager fm, List<Class<? extends Fragment>> list,
				Map<Integer, Map<String, T>> args) {
			super(fm);
			listClass = list;
			argList = args;
		}

		@Override
		public Fragment getItem(int position) {
			Fragment child = mFragmentList.get(position);
			if (child == null) {
				child = createNewFragment(position, listClass.get(position),
						argList == null ? null : argList.get(position));
			}
			return child;
		}

		@Override
		public void setPrimaryItem(ViewGroup container, int position, Object object) {
			super.setPrimaryItem(container, position, object);
			mFragmentList.put(position, (Fragment) object);
		}

		@Override
		public int getCount() {
			return listClass.size();
		}
	}

	private <T> Fragment createNewFragment(int position, Class<? extends Fragment> cls, Map<String, T> args) {
		try {
			Fragment fragment = cls.newInstance();
			if (args != null && args.size() != 0) {
				Iterator<Entry<String, T>> it = args.entrySet().iterator();
				while (it.hasNext()) {
					Entry<String, T> temp = it.next();
					String key = temp.getKey();
					T value = temp.getValue();
					try {
						Method method = cls.getMethod(key, value.getClass());
						try {
							method.invoke(fragment, value);
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						}
					} catch (SecurityException e) {
						e.printStackTrace();
					} catch (NoSuchMethodException e) {
						e.printStackTrace();
					}
				}
			}
			mFragmentList.put(position, fragment);
			return fragment;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private class MyOnPageChangeListener implements LazyViewPager.OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int state) {

		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

		}

		@Override
		public void onPageSelected(int position) {
			if (mType != TYPE_BUTTOM_NO_SCROLL && mType != TYPE_MIDDLE_ACTION) {
				changeText(position);
				setImagePosition(position);
			} else {
				setCurrentBottomTab(position);
			}
			onPageChanged(position);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		refreshTexts();
	}

	public final Fragment getFragment(int position) {
		return mAdapter != null ? mAdapter.getItem(position) : null;
	}

	// public final Fragment getFragment(int position) {
	// return mFragmentList.get(position);
	// }

	public final boolean isFragmentListNull() {
		return mFragmentList == null || mFragmentList.size() == 0;
	}

	public final int getFragmentSize() {
		if (mFragmentList == null) {
			return 0;
		} else {
			return mFragmentList.size();
		}
	}

	public final void setViewPagerLimit(int size) {
		mViewPager.setOffscreenPageLimit(size);
	}

	public final void setTabBgAlpha(int alpha) {
		switch (mType) {
		case TYPE_NORMAL:
		case TYPE_ROUND_POINT:
		case TYPE_TOP_NO_SCROLL:
			mTxtContent.getBackground().setAlpha(alpha);
			break;
		case TYPE_BUTTOM_NO_SCROLL:
			mButtonTabs.getBackground().setAlpha(alpha);
			break;
		case TYPE_MIDDLE_ACTION:
			mBtnContainer.getBackground().setAlpha(alpha);
			break;
		default:
			break;
		}
	}

	public final int getPos() {
		return mCurrentPosition;
	}

	public final void setCurrentTab(int position) {
		if (position >= 0 && position < mViewPager.getAdapter().getCount()) {
			mViewPager.setCurrentItem(position);
			if (mType == TYPE_MIDDLE_ACTION) {
				mCurrentPosition = position;
			}
		}
	}

	/**
	 * 是否需要隐藏顶部tab栏
	 * 
	 * @param visibility
	 */
	public final void setTopTabVisibility(int visibility) {
		mTopTabContent.setVisibility(visibility);
	}

	/**
	 * 是否需要隐藏底部tab栏
	 * 
	 * @param visibility
	 */
	public final void setBottomTabVisibility(int visibility) {
		mButtomContent.setVisibility(visibility);
	}

	@Override
	protected boolean needSetActivityBackground() {
		return false;
	}

	protected void setTabActivityBackground() {
		if (needSetTabActivityBackground()) {
			getBaseContent().setBackgroundColor(ResourcesUtil.getColor(R.color.common_grey_bg_color));
		}
	}

	protected boolean needSetTabActivityBackground() {
		return true;
	}
}
